package org.tony.utils;

import java.util.HashMap;
import java.util.Map;

import org.tony.db.DbType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 触发器模板初始化类
 * @author tony
 *
 */
public class TriggerTemplateInitial {
	
	
	private static final String TPL_PATH = "res/trigger-template.xml" ;
	private static final Map<String,String> triggerTemplates = new HashMap<String,String>();
	
	private static TriggerTemplateInitial instance = new TriggerTemplateInitial() ;
	private TriggerTemplateInitial(){
	}
	
	public static  TriggerTemplateInitial getInstance() {
		return instance;
	}
	
	public synchronized void init() {
		try {
			Document doc = XmlDataSourceHelper.getDocuemnt(TPL_PATH) ;
			Element root = doc.getDocumentElement() ;
			NodeList nodes = XmlDataSourceHelper.queryNodes("/triggers/trigger", root);
			if(nodes!=null && nodes.getLength()>0) {
				for(int i=0;i<nodes.getLength();i++) {
					Node cur = nodes.item(i) ;
					String dbtype = cur.getAttributes().getNamedItem("dbtype").getNodeValue() ;
					String value = cur.getFirstChild().getNodeValue() ;
					triggerTemplates.put(dbtype.trim().toUpperCase(), value) ;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getTemplate(String type) {
		return triggerTemplates.get(type.toUpperCase());
	}
	
	public static void main(String[] args) {
		System.out.println(TriggerTemplateInitial.getInstance().getTemplate(DbType.ORACLE.name().trim()));
	}
}
